<?php
/**
 * Created by PhpStorm.
 * User: ttl
 * Date: 2019/5/20
 * Time: 11:15
 */

namespace app\common\logic;
use think\Db;
use think\Validate;

class BuildsLogic
{
    /**
     * 获得设置配置
     */
    public function get_build_config(){
        $build_types = Db::name('BuildType')->cache(true,3600)->select();
        $regions = Db::name('Region')->cache(true,3600)->select();
        $big_types = Db::name('BigType')->cache(true,3600)->select();
        return ['build_types'=>$build_types,'regions'=>$regions,'big_types'=>$big_types];
    }

    public function get_builds(){
        $build_type = input('build_type/d',0);
        $region = input('region/d',0);
        $big_type = input('big_type/d',0);

        $where = [];
        if($build_type){
            $where['build_type'] = $build_type;
        }

        if($region){
            $where['region_id'] = $region;
        }

        if($big_type){
            $where['big_type'] = $big_type;
        }
        $where['state'] = 1;

        $result = Db::name('Builds')->where($where)->field('id,names,build_type,region_id,big_type,longitude,latitude')->select();
        return $result;
    }

    public function build_msg(){
        $build_id = input('build_id/d',0);
        $build_img = Db::name('BuildsImg')->where(['build_id'=>$build_id])->select();
        $build = Db::name('Builds')->where(['id'=>$build_id])->find();
        $commont_num = Db::name('Comment')->count();

        return ['build_imgs'=>$build_img,'build_msg'=>$build,'commont_num'=>$commont_num];
    }

    /**
     * 查找评论列表
     * @return array
     */
    public function build_commits(){
        $build_id = input('build_id/d',0);
        $pageid = input('pageid/d',0);
        $where['build_id'] = $build_id;
        $where['state'] = 1;
        $comment = Db::name('Comment')->where($where)->page($pageid,10)
        ->order('id desc')->select();

//        return $comment;
        $comment_num = count($comment);
        if($comment_num>0){
            $end_id = $comment[0]['id'];
            $start_id = $comment[$comment_num-1]['id'];
            $iwhere['comment_id'] = ['BETWEEN',[$start_id,$end_id]];

            $comment_img = Db::name('CommentImg')->where($iwhere)->select();
            return ['comment'=>$comment,'img'=>$comment_img,'start_id'=>$start_id,'end_id'=>$end_id];//$comment_img;
        }

        return ['comment'=>[],'img'=>[]];
    }

    /**
     * 提交评论
     */
    public function submit_commit($user){

        $rule = [
            'build_id'  => 'require|number',
            'msg'   => 'require|length:1,200',
        ];

        $msg = [
            'build_id.require' => '请填写对应设施编号',
            'build_id.number'     => '设施编号必须是数字',
            'msg.require'   => '请填写对应评论',
            'msg.length'  => '请输入1-200字的评论'
        ];

        $user_db = Db::name("User")->where(['id'=>$user['id']])->find();
        $comment_time = $user_db['comment_time'];
        $cur_time = time();
        if($cur_time - $comment_time<5){
            return ['status'=>'-1','msg'=>'提交过于频繁','result'=>[]];
        }

        $v_data = input('post.');
        $build_id = input('build_id/d',0);
        $validate = new Validate($rule, $msg);
        $result   = $validate->check($v_data);
        if(!$result){
            return ['status'=>'-1','msg'=>$validate->getError(),'result'=>[]];
        }

        $img_arr = input('imgarr/a',[]);

        if(count($img_arr)>3){
            return ['status'=>'-1','msg'=>'评论最多只能传3张图片','result'=>[]];
        }

        $comm_id = Db::name('comment')
            ->insertGetId(['user_name'=>$user_db['nickname'],
                'user_icon'=>$user_db['headicon'],
                'user_id'=>$user_db['id'],
                'build_id'=>$build_id,
                'msg'=>$v_data['msg']]);

        if(!$comm_id){
            return ['status'=>'-1','msg'=>'添加失败','result'=>[]];
        }


        $add_data = [];
        for ($i=0;$i<count($img_arr);$i++) {
            if(!$img_arr[$i])
                continue;
            array_push($add_data,['comment_id'=>$comm_id,'urls'=>$img_arr[$i]]);
        }
        Db::name('commentImg')->insertAll($add_data);
        Db::name('User')->where(['id'=>$user_db['id']])->update(['comment_time'=>time()]);

        return ['status'=>'1','msg'=>'','result'=>['comm_id'=>$comm_id]];
    }

    public function add_builds(){


    }
}